package com.example.siyun.controller.filecontroller;

import com.example.siyun.util.*;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UploadController {


    @GetMapping("/index")
    public String index() {
        return "upload";
    }
    /**
     * 上传视频的接口
     * @ruturn 返回上传到文件服务器的路径
     */
    @PostMapping("/shipin/upload") //new annotation since 4.3
    public String fileUpload(@RequestParam("file") MultipartFile file
                             )

    { String path = null;
        if (file.isEmpty()) {

        }
        try {
            // 就用byte数组传输吧
            byte[] file_buff = null;
            InputStream inputStream=file.getInputStream();
            if(inputStream!=null){
                int len1 = inputStream.available();
                file_buff = new byte[len1];
                inputStream.read(file_buff);
            }
            inputStream.close();
            String[] results= FastDFSUtils.upload(file_buff);

            //拼接图片访问地址
            path = FastDFSClienta.getUrl()+results[0]+ "/"+results[1];
        } catch (Exception e) {

        }
        return path;
    }
    @RequestMapping("/intermediate/upload")
    public Map<String,Object> uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest httpServletRequest) throws IOException {
        Map<String, Object> map = new HashMap<>();
        FastUplodFile fastUplodFile = new FastUplodFile();
        Map<String, String> map1 = fastUplodFile.uploadFile(file);
        httpServletRequest.getSession().setAttribute("process",25);
        String path = map1.get("path");
        String path1 = map1.get("path1");
        VideoUtils videoUtils = new VideoUtils();
        String mp4 = videoUtils.videoConvert(path, path1, 1280, 720, "mp4");
        httpServletRequest.getSession().setAttribute("process",75);
        String fileName = mp4;
        String filePath = path1 + "/" + mp4;
        MultipartFile file1 = new MockMultipartFile(fileName, new FileInputStream(filePath));
        UploadController uploadController = new UploadController();
        String s = uploadController.fileUpload(file1);
        File file2 = new File(path1);
        long result = VideoUtil.getDuration(filePath);
        System.out.println(s);
        map.put("path",s);
        map.put("time",result);
        map.put("code",0);
        System.out.println(result);
        System.out.println(s);
        FileDelete fileDelete = new FileDelete();
        fileDelete.delete(file2);
        httpServletRequest.getSession().setAttribute("process",100);
        return map;

    }
}
